#include "Creature.hpp"

Creature::Creature(App* appPtr) : Entity(appPtr)
{
	m_mouvementAnim.resize(static_cast<int>(CREATURE_DIE) + 1, NULL);
	m_currentMouvement = CREATURE_IDLE;

}
// Methode Get

int Creature::getHealth()
{
	return m_health;
}

CreatureType Creature::getType()
{
	return m_type;
}

CreatureThreat Creature::getThreat()
{
	return m_threat;
}

CreatureElementalType Creature::getElementalType()
{
	return m_elemental_type;
}

// Methode Set

void Creature::setHealth(int health)
{
	m_health = health;
}

void Creature::setType(CreatureType type)
{
	m_type = type;
}

void Creature::setThreat(CreatureThreat threat)
{
	m_threat = threat;
}

void Creature::setElementalType(CreatureElementalType ElementalType)
{
	m_elemental_type = ElementalType;
}

// Other Methode

void Creature::damage(int amount)
{
	m_health -= amount;
}

void Creature::heal(int amount)
{
	m_health += amount;
}

void Creature::AddAnimation(CreatureMouvementType type, Animation* animation)
{
	m_mouvementAnim[static_cast<int>(type)] = animation;

}

void Creature::RemoveAnimation(CreatureMouvementType type)
{
	Animation* animPtr = m_mouvementAnim[static_cast<int>(type)];

	delete animPtr;
	animPtr = NULL;

	m_mouvementAnim[static_cast<int>(type)] = NULL;
}

Animation* Creature::CurrentAnimation()
{
	Animation* animPtr = m_mouvementAnim[static_cast<int>(m_currentMouvement)];

	return animPtr;
}

void Creature::SetAnimation(CreatureMouvementType type)
{
	m_currentMouvement = type;
}

void Creature::FlipX(bool flipped)
{
    for(int i = 0; i < m_mouvementAnim.size(); i++)
    {
        if(m_mouvementAnim[i] != NULL)
            m_mouvementAnim[i]->FlipX(flipped);
    }
}

void Creature::LoadScript(const std::string &filename)
{
    std::queue<TagValues> values;
    FileParser::AllValues(filename, values);
    std::string texte;

    // Initialize resource
    ImageRessource *_resource = NULL;
    while(values.size() != 0)
    {
        if(values.front().first == "Name")
        {
            texte = values.front().second.at(0);
        }
        else if(values.front().first == "ID")
        {
            SetID(FileParser::ToInt(values.front().second.at(0)));
            m_mouvementAnim[static_cast<int>(ID())] = new Animation();
        }
        else if(values.front().first == "Anim.Load")
        {
            _resource = GetApp()->m_ressourceManager.AddImage("Anim_" + texte, values.front().second.at(0));
            _resource->SetSmoothImage(false);
        }
        else if(values.front().first == "Anim.Delay")
        {
            m_mouvementAnim[static_cast<int>(ID())]->SetFrameTime(FileParser::ToFloat(values.front().second.at(0)));
        }
        if(values.front().first == "Anim.AddFrame")
        {
            if(values.front().second.size() == 4)
            {
                sf::IntRect subRect;
                subRect.Left = FileParser::ToInt(values.front().second.at(0));
                subRect.Top = FileParser::ToInt(values.front().second.at(1));
                subRect.Right = FileParser::ToInt(values.front().second.at(2));
                subRect.Bottom = FileParser::ToInt(values.front().second.at(3));
                Frame* frame = new Frame(_resource, subRect);
                m_mouvementAnim[static_cast<int>(ID())]->AddFrame(frame);
            }
        }

        values.pop();
    }

    std::cout << "Script : " << filename << " loaded ! " << std::endl;
}
